//
//  SubjectCell.m
//  LimitFree
//
//  Created by LZXuan on 15-6-27.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "SubjectCell.h"
#import "SubjectAppView.h"
#import "UIImageView+WebCache.h"


@interface SubjectCell ()
@property (nonatomic,copy) SubjectBlock myBlock;
@property (nonatomic,strong) SubjectModel *model;
@end

@implementation SubjectCell

- (void)awakeFromNib {
    //没有选中效果
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // Initialization code
    //初始化的时候 创建 四个 SubjectAppView
    CGFloat h = self.imgImageView.frame.size.height/4;
    CGFloat x = self.imgImageView.frame.origin.x+self.imgImageView.frame.size.width;
    CGFloat w = self.frame.size.width-x;
    CGFloat y = self.imgImageView.frame.origin.y;
    
    for (NSInteger i  = 0; i < 4; i++) {
        //去加载xib  中 的视图
        SubjectAppView *appView = [[[NSBundle mainBundle] loadNibNamed:@"SubjectAppView" owner:nil options:nil] lastObject];
        appView.tag = 201+i;
        appView.frame = CGRectMake(x, y+h*i, w, h);
        //增加 点击事件
        [appView addTarget:self action:@selector(appViewClick:)];
        [self.contentView addSubview:appView];
    }
}
- (void)appViewClick:(SubjectAppView *)appview {
    //实现 界面跳转 跳转到详情
    //cell 委托 所在的视图控制器 实现界面跳转
    //需要采用 代理设计模式
    //block 回调的形式实现
    
    //SubjectModel 中有一个数组，数组中存放的四个AppView 的数据
    if (self.myBlock) {
        //传值  appId
        AppModel *appModel = self.model.applications[appview.tag-201];
        //调用block
        self.myBlock(appModel.applicationId);
    }
}
- (void)showDataWithSubjectModel:(SubjectModel *)model myBlock:(SubjectBlock)block {
    //先保存  model 和block
    self.model = model;
    self.myBlock = block;
    //////////////////////
    self.nameLabel.text = model.title;
    [self.imgImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed: @"topic_TopicImage_Default"]];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.desc_img] placeholderImage:[UIImage imageNamed: @"topic_Header"]];
    self.textView.text = model.desc;
    ///////////
    //填充4个appView
    for (NSInteger i = 0; i < model.applications.count; i++) {
        SubjectAppView *appview = (SubjectAppView *)[self.contentView viewWithTag:201+i];
        //获取AppModel
        AppModel *appModel = model.applications[i];//数组中存的都是AppModel
        
        [appview showDataWithModel:appModel];
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
